package org.dromara.system.kernel;

import org.dromara.common.core.utils.StringUtils;
import org.dromara.system.domain.PayOrder;
import org.dromara.system.domain.vo.PayGroupTypeVo;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;

/**
 * 价格插件内核
 *
 * @author Gavino
 */
public class PricePluginKernel {

    public static void setPrice(PayOrder order, PayGroupTypeVo groupType) {
        String pricePluginStr = groupType.getPricePlugin();
        Integer priceForce = groupType.getPriceForce();
        if (StringUtils.isNotBlank(pricePluginStr)) {
            try {
                String[] pricePlugins = pricePluginStr.split("\n");
                for (String pricePlugin : pricePlugins) {
                    String[] pricePluginArr = pricePlugin.split(":");
                    if (pricePluginArr.length == 2) {
                        String[] prices = pricePluginArr[0].split("-");
                        BigDecimal minMoney = new BigDecimal(prices[0]);
                        BigDecimal maxMoney = new BigDecimal(prices[1]);
                        if (order.getUsdMoney().compareTo(minMoney) >= 0 && order.getOrderMoney().compareTo(maxMoney) < 0) {
                            order.setUsdMoney(new BigDecimal(pricePluginArr[1]));
                            order.setOrderMoney(order.getUsdMoney().multiply(order.getRates()).setScale(2, RoundingMode.HALF_UP));

                            //是否强制修改订单金额
                            if (Objects.equals(priceForce, 1)) {
                                order.setCUsdMoney(new BigDecimal(pricePluginArr[1]));
                                order.setCMoney(order.getOrderMoney());
                            }
                            break;
                        }
                    }
                }
            } catch (Exception ignore) {
            }
        }
    }

    public static void setPriceV2(PayOrder order, PayGroupTypeVo groupType) {
        String pricePluginStr = groupType.getPricePlugin();
        Integer priceForce = groupType.getPriceForce();
        if (StringUtils.isNotBlank(pricePluginStr) && !pricePluginStr.contains("\n") && !pricePluginStr.contains(":")) {
            try {
                String[] prices = pricePluginStr.split("-");
                BigDecimal minMoney = new BigDecimal(prices[0]);
                BigDecimal maxMoney = new BigDecimal(prices[1]);

                BigDecimal usdMoney = order.getUsdMoney();
                if (order.getUsdMoney().compareTo(minMoney) < 0) {
                    usdMoney = minMoney;
                } else if (order.getUsdMoney().compareTo(maxMoney) > 0) {
                    usdMoney = maxMoney;
                }

                if (usdMoney.compareTo(order.getUsdMoney()) != 0) {
                    order.setUsdMoney(usdMoney);
                    order.setOrderMoney(order.getUsdMoney().multiply(order.getRates()).setScale(2, RoundingMode.HALF_UP));

                    //是否强制修改订单金额
                    if (Objects.equals(priceForce, 1)) {
                        order.setCUsdMoney(order.getUsdMoney());
                        order.setCMoney(order.getOrderMoney());
                    }
                }
            } catch (Exception ignore) {
            }
        }
    }
}
